<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Register your Y account</title>
    <link rel="stylesheet" href="css/loginstyle.css">
    <link rel="icon" href="img/favicon.png" type="image/x-icon">
    <link rel="shortcut icon" href="img/favicon.png" type="image/x-icon">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<div class="login-box">
    <h1>Register your Y account</h1>
    <form id="registerForm">
        <div class="form-group">
            <label for="username">Username</label>
            <input type="text" id="username" name="username" placeholder="Username" required>
        </div>
        <div class="form-group">
            <label for="email">Email</label>
            <input type="email" id="email" name="email" placeholder="Email" required>
        </div>
        <div class="form-group">
            <label for="password">Password</label>
            <input type="password" id="password" name="password" placeholder="Password" required>
        </div>
        <div class="form-group">
            <label for="confirmPassword">Confirm Password</label>
            <input type="password" id="confirmPassword" name="confirmPassword" placeholder="Confirm Password" required>
        </div>
        <div class="form-group">
            <input type="checkbox" id="terms" name="terms" required>
            <label for="terms">
                I agree with the <a href="#">Terms of Service</a> and <a href="#">Privacy Policy</a>
            </label>
        </div>
        <button type="submit">Register</button>
        <p>Already have an account? <a href="./login.html">Sign In</a></p>
    </form>
</div>

<script>
    $(document).ready(function() {
        $('#registerForm').submit(function(event) {
            event.preventDefault();

            var formData = {
                username: $('#username').val(),
                email: $('#email').val(),
                password: $('#password').val(),
                confirmPassword: $('#confirmPassword').val(),
                terms: $('#terms').is(':checked')
            };

            $.ajax({
                type: 'POST',
                url: '/register',
                data: JSON.stringify(formData),
                contentType: 'application/json',
                success: function(response) {
                    if (response.status === 'no_user') {
                        alert('无此用户，请先注册或重新输入');
                    } else if (response.status === 'wrong_password') {
                        alert('密码错误');
                    } else if (response.status === 'success') {
                        alert('登陆成功');
                        window.location.href = 'index.html';
                    }
                },
                error: function(xhr, status, error) {
                    alert('请求失败: ' + xhr.responseText);
                }
            });
        });
    });
</script>
</body>
</html>
